<template>
  <div class="special-column-main">
    <!-- 左侧主内容区 -->
    <div class="left-container">
      <!-- 分类导航 -->
      <div class="category-section">
        <el-card class="category-card" :body-style="{ padding: '20px' }">
          <div class="category-header">
            <h3>文章分类</h3>
            <el-button link type="primary">全部分类</el-button>
          </div>
          <div class="category-list">
            <div
              v-for="(item, index) in categoryList"
              :key="index"
              class="category-item"
              :class="{ active: item.categoryId === categoryId }"
            >
              <el-button
                :type="item.categoryId === categoryId ? 'primary' : 'default'"
                class="category-btn"
                @click="refreshPage(item.categoryId)"
              >
                {{ item.categoryName }}
                <el-badge
                  :value="item.count"
                  :type="item.categoryId === categoryId ? 'primary' : 'info'"
                  class="category-count"
                />
              </el-button>
            </div>
          </div>
        </el-card>
      </div>

      <!-- 文章列表 -->
      <div v-loading="loading" class="blog-list-section">
        <el-card
          v-for="(item, index) in blogCardList"
          :key="index"
          class="blog-card"
          :body-style="{ padding: '20px' }"
        >
          <div class="blog-card-content">
            <!-- 文章封面图 -->
            <div v-if="item.blogCoverImage" class="blog-cover">
              <el-image :src="item.blogCoverImage" fit="cover" loading="lazy" />
            </div>

            <!-- 文章信息 -->
            <div class="blog-info">
              <!-- 标题 -->
              <router-link
                :to="{ name: 'blogViewShow', params: { blogId: item.blogId } }"
                class="blog-title"
              >
                <h2>{{ item.blogTitle }}</h2>
              </router-link>

              <!-- 摘要 -->
              <p class="blog-summary">
                {{ item.blogContent }}
              </p>

              <!-- 文章元信息 -->
              <div class="blog-meta">
                <div class="meta-left">
                  <div class="author-info">
                    <el-avatar :size="24" :src="item.avatar" />
                    <span class="author-name">{{ item.author }}</span>
                  </div>
                  <span class="publish-time">
                    <el-icon><clock /></el-icon>
                    {{ item.createTime }}
                  </span>
                </div>
                <div class="meta-right">
                  <span class="meta-item">
                    <el-icon><view /></el-icon>
                    {{ item.blogViews }}
                  </span>
                  <span class="meta-item">
                    <el-icon><star /></el-icon>
                    {{ item.likes }}
                  </span>
                  <router-link
                    :to="{
                      name: 'blogListShow',
                      params: { categoryId: item.blogCategoryId }
                    }"
                    class="category-link"
                  >
                    <el-tag size="small" effect="light" round>
                      {{ item.blogCategoryName }}
                    </el-tag>
                  </router-link>
                </div>
              </div>
            </div>
          </div>
        </el-card>

        <!-- 分页 -->
        <div class="pagination-container">
          <pagination
            v-if="total > 0"
            v-model:page="listQuery.pageNum"
            v-model:limit="listQuery.pageSize"
            :page-sizes="[5, 10, 30, 50]"
            :total="total"
            @pagination="paginationChange"
          />
        </div>
      </div>
    </div>

    <!-- 右侧边栏 -->
    <div class="right-container">
      <el-affix :offset="61" target=".right-container">
        <statistic-card />
      </el-affix>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { Clock, View, Star } from '@element-plus/icons-vue';
import Pagination from '@/components/common/pagination/index.vue';
import StatisticCard from '@/components/common/statistic-card/index.vue';
import { pageBlogCards } from '@/api/blog';
import { useRoute, useRouter } from 'vue-router';
import { BlogInfo } from '@/api/blog/types';
import { getCategoryStatistics } from '@/api/category';
import { BlogCategoryStatisticsVO } from '@/api/category/types';
import { useUserStore } from '@/store/modules/user';

const router = useRouter();
const UserStore = useUserStore();

const loading = ref(false);
const listQuery = ref({
  pageNum: 1,
  pageSize: 20
});
const total = ref(0);
// 文章卡片列表
const blogCardList = ref([] as BlogInfo[]);
const categoryId = ref('');

const search = () => {
  loading.value = true;
  pageBlogCards({
    ...listQuery.value,
    categoryId: categoryId.value || undefined,
    userId: UserStore.userInfo.id?.toString() || undefined
  }).then(response => {
    if (response.data && Array.isArray(response.data.list)) {
      blogCardList.value = response.data.list;
      total.value = Number(response.data.total) || 0;
    }
    loading.value = false;
  });
};

const refreshPage = (id: string) => {
  categoryId.value = id;
  search();
};

// 文章分类列表
const categoryList = ref([] as BlogCategoryStatisticsVO[]);

// 获取分类统计
const getCategoryStatistic = () => {
  getCategoryStatistics().then(response => {
    categoryList.value = response.data as [];
  });
};

// 初始化
onMounted(() => {
  search();
  getCategoryStatistic();
});

const paginationChange = ({ page, limit }: { page: number; limit: number }) => {
  listQuery.value.pageNum = page;
  listQuery.value.pageSize = limit || 20;
  search();
};
</script>

<style scoped lang="scss">
.special-column-main {
  width: 100%;
  display: flex;
  gap: 24px;
  padding: 24px;
  background-color: var(--el-bg-color-page);
  min-height: calc(100vh - 60px);

  .left-container {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 24px;
  }

  .right-container {
    width: 240px;
    flex-shrink: 0;
  }
}

// 分类导航样式
.category-section {
  .category-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;

    h3 {
      margin: 0;
      font-size: 18px;
      font-weight: 600;
      color: var(--el-text-color-primary);
    }
  }

  .category-list {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
  }

  .category-item {
    .category-btn {
      display: flex;
      align-items: center;
      gap: 8px;
      height: 32px;
      padding: 0 16px;
      border-radius: 16px;
      transition: all 0.3s;

      &:hover {
        transform: translateY(-2px);
      }
    }

    .category-count {
      margin-left: 4px;
    }
  }
}

// 文章卡片样式
.blog-card {
  transition: all 0.3s;
  margin-bottom: 16px;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  }

  .blog-card-content {
    display: flex;
    gap: 20px;
  }

  .blog-cover {
    width: 200px;
    height: 134px;
    border-radius: 8px;
    overflow: hidden;
    flex-shrink: 0;

    .el-image {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }

  .blog-info {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
  }

  .blog-title {
    text-decoration: none;

    h2 {
      margin: 0 0 12px;
      font-size: 18px;
      font-weight: 600;
      color: var(--el-text-color-primary);
      line-height: 1.4;
      display: -webkit-box;
      -webkit-line-clamp: 1;
      -webkit-box-orient: vertical;
      overflow: hidden;
    }
  }

  .blog-summary {
    margin: 0 0 16px;
    font-size: 14px;
    color: var(--el-text-color-regular);
    line-height: 1.6;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }

  .blog-meta {
    margin-top: auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 13px;
    color: var(--el-text-color-secondary);

    .meta-left,
    .meta-right {
      display: flex;
      align-items: center;
      gap: 16px;
    }

    .author-info {
      display: flex;
      align-items: center;
      gap: 8px;
    }

    .publish-time {
      display: flex;
      align-items: center;
      gap: 4px;
    }

    .meta-item {
      display: flex;
      align-items: center;
      gap: 4px;
    }

    .category-link {
      text-decoration: none;
    }
  }
}

// 分页容器样式
.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 24px;
}

// 响应式设计
@media (max-width: 1200px) {
  .special-column-main {
    flex-direction: column;

    .right-container {
      width: 100%;
    }
  }
}

@media (max-width: 768px) {
  .blog-card {
    .blog-card-content {
      flex-direction: column;
    }

    .blog-cover {
      width: 100%;
      height: 200px;
    }
  }
}
</style>
